/**
  ******************************************************************************
  * @file
  * @brief
  * @author luffy
  * @version V1.0.0
  * @date 2022/12/16
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "misc.h"
#include "stm32f4xx_rcc.h"
#include "A_memory.h"

#include "system.h"
#include "communication.h"
#include "pressuresensor.h"
#include "led_task.h"
#include "lncup_ctl_task.h"
#include "valve_ctl_task.h"
#include "monitor_task.h"
#include "step_motor.h"
#include "pfc_task.h"
#include "task_mng.h"
#include "ParmManage.h"
#include "dev_bag302.h"
#include "main_task.h"
#include "drv_f4_timer_int.h"
#include "A_memory_shell.h"
#include "A_backtrace.h"
#include "A_aclass_protocol.h"
/* Private defines -----------------------------------------------------------*/
#define Start_TASK_PRIO     2                         //任务优先级
#define Start_STK_SIZE      800                       //任务堆栈大小
#define APP_NAME 					"CmBacktrace"
#define HARD_WARE_VER 		"V1.0.0"
#define SOFT_WARE_VER 		"V1.0.3"

TaskHandle_t                StartTask_Handler;        //任务句柄
SemaphoreHandle_t 			xNotifyReadBasePressSemaphore;

/* Private Struct  -----------------------------------------------------------*/

/* Private enum    -----------------------------------------------------------*/

/* Private Variable  ---------------------------------------------------------*/
//Pointer
//Array

//Const
/* Private function prototypes -----------------------------------------------*/

/***********************************************************************************
 * @brief 
 * ex:
 * @par  
 * None
 * @par 
 * None
 * @retval
 **********************************************************************************/
extern EventGroupHandle_t xSensorEventGroup;
static void Startup(void *pvParameters)
{
	vTaskDelay(pdMS_TO_TICKS(500));

	//必要的初始化
	system_init();
	AbacktraceInit(APP_NAME, HARD_WARE_VER, SOFT_WARE_VER);
	communication_init();
	DataServer_Init();

    //CLASS_LOG_I("finished init\r\n");
	
    //压力读取信号量创建
	xNotifyReadBasePressSemaphore = xSemaphoreCreateBinary();
	xSemaphoreGive(xNotifyReadBasePressSemaphore);
	
    xSensorEventGroup = xEventGroupCreate();
	//任务初始化
	task_mng_init();

    // 初始化定时器，设置1分钟超时
    InitHandshakeTimer();
	
    //上电读取设备运行状态
    PPItemRead(PP_DEV_RUNSTATE, (uint8_t *)(&test_state), 1);
    
    #if 1
    if(test_state == RUNING)
    {
        Ashell_print("the experiment did not end normally before the power off\r\n");
    }
    else
    {
        Ashell_print("the experiment end normally before the power off\r\n");
    }
    #endif
    
    //上电后读取体积
    PPItemRead(PP_BASE_CBM, (uint8_t *)(&data_server.base_volume), 4);
	Ashell_print("base volume is %f cm3 \r\n",data_server.base_volume);
    
    //上电后如果读出设备状态为运行，写入断电标志
	if(RUNING == test_state)
	{
		poweroff_state = POWER_OFF_ON_RUNING;
        Ashell_print("write power off flag\r\n");
        PPItemIWrite(PP_POWER_OFF_FLAG, (uint8_t *)(&poweroff_state), 1);
	}
	else
	{
        poweroff_state = POWER_OFF_ON_FINISHED;
        Ashell_print("unwrite power off flag\r\n");
		PPItemIWrite(PP_POWER_OFF_FLAG, (uint8_t *)(&poweroff_state), 0);
	}

	vTaskDelete(StartTask_Handler);
}
/***********************************************************************************
 * @brief 
 * ex:
 * @par  
 * None
 * @par 
 * None
 * @retval
 **********************************************************************************/
int main(void)
{	
	// 设置中断向量表
	SCB->VTOR = (uint32_t)STORAGE_APPLICATION_BASE;       //与options Target IROM1 Strat一致
	FLASH_SetLatency(FLASH_Latency_5);                    //等待5个机器周期   因为HCLK为168MHZ
	// 设置中断优先级分组
	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

	// 获取当前时钟频率
	RCC_ClocksTypeDef get_rcc_clock;
	RCC_GetClocksFreq(&get_rcc_clock);

	// 创建启动任务                                            
	xTaskCreate(Startup, "Startup", Start_STK_SIZE, NULL, Start_TASK_PRIO, &StartTask_Handler);

    
	// 启动调度器
	vTaskStartScheduler();

	return 0;
}

//空闲钩子函数
void vApplicationIdleHook(void) 
{

}


/******************* (C) COPYRIGHT 2022 CIQTEK luffy *****END OF FILE****/
